package com.bkgy.controller;

import com.baosight.hdsdk.exception.HDSdkException;
import com.bkgy.modular.calsidata.service.SiDataBX1Service;
import com.bkgy.modular.calsidata.service.Supplementarydata;
import com.bkgy.modular.datacollect_bx.bean.JDataSavemong2280_1;
import com.bkgy.sys.common.entity.ResultData;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Tag(name = "test", description = "测试")
@Controller
@RequestMapping("test")
public class TestController {

    @Resource
    private SiDataBX1Service siDataBX1Service;

    @Resource
    private Supplementarydata supplementarydata;
    @Resource
    private MongoTemplate mongoTemplate;

    @GetMapping("/tezheng")
    @ResponseBody
    public ResultData<String> tezheng(String extid) {
        //ben_cold_curve_cgl1870
        Query query = new Query();
        query.addCriteria(Criteria.where("flag").is(1));
        query.addCriteria(Criteria.where("extid").is(extid));
        String[] arr = new String[]{
                "tcm_process_speed_avg", "tcm_tank1_temp_avg", "tcm_tank2_temp_avg",
                "tcm_tank3_temp_avg", "tcm_rinse_ph_avg", "tcm_dry_temp_avg",
                "tcm_tank1_emu_temp_avg", "tcm_tank2_emu_temp_avg", "tcm_tank3_emu_temp_avg",
                "tcm_1#_ru_pressure_avg", "tcm_2#_ru_pressure_avg", "tcm_3#_ru_pressure_avg",
                "tcm_4#_ru_pressure_avg", "tcm_5#_ru_pressure_avg", "tcm_s5_strip_speed_act_avg"};
        for (int i = 0; i < arr.length; i++) {
            query.fields().include("trailValues." + arr[i]);
        }
        JDataSavemong2280_1 mongoData = mongoTemplate.findOne(query, JDataSavemong2280_1.class);
//        JDataSavemong1870 mongoData = mongoTemplate.findOne(query, JDataSavemong1870.class);
        for (int i = 0; i < arr.length; i++) {
            if (!mongoData.getTrailValues().containsKey(arr[i])) {
                log.info("{} 变量数据不存在", arr[i]);
            }
        }

        return ResultData.ResultDataSuccess(mongoData);
    }

    @CrossOrigin
    @GetMapping("/getCurve")
    @ResponseBody
    public ResultData<String> getCurve(@RequestParam(required = false) String extid, @RequestParam(required = false) String var_name) {
        Query query = new Query(Criteria.where("extid").is(extid).and("var_name").is(var_name));
        //寻找曲线数据 extid=5BW320900000 var_name:tcm_dry_temp
        JDataSavemong2280_1 obj = mongoTemplate.findOne(query, JDataSavemong2280_1.class);
        //寻找曲线数据 extid=54H333470000，var_name:cgl_oil_amount
        //JDataSavemong1870 obj = mongoTemplate.findOne(query, JDataSavemong1870.class);

        List<Double> sList = new ArrayList<>();
        if (obj != null) {
            for (Double data : obj.getCurve()) {
                sList.add(data);
            }
        }
        return ResultData.ResultDataSuccess(sList, sList.size());
    }

    @GetMapping("html")
    public String html() {
        return "test";
    }

    @CrossOrigin
    @RequestMapping(value = {"/tt"}, method = RequestMethod.POST)
    @ResponseBody
    public String tt(@RequestBody String data) {
        System.out.println(data);
        return "success";
    }

    @CrossOrigin
    @RequestMapping(value = {"/test"}, method = RequestMethod.POST)
    @ResponseBody
    public void xx() throws HDSdkException, ParseException {
        siDataBX1Service.saveSiData();
    }

    @CrossOrigin
    @RequestMapping(value = {"/test_ben"}, method = RequestMethod.POST)
    @ResponseBody
    public void xx1() throws HDSdkException, ParseException, SftpException, IOException, JSchException {
        supplementarydata.test_ben();
    }
}
